//
//  MyRedenvelopesUsed.m
//  NewPro
//
//  Created by 何荧红 on 2019/6/21.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "MyRedenvelopesUsed.h"
#import "MyRedenvelopesCell.h"

#import "MyRedenvelopesModel.h"
#import "MyRedenvelopesStore.h"

@interface MyRedenvelopesUsed ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) PushView *pushView;
@property (nonatomic, copy) NSString *RedStatus;

@property (nonatomic, assign) NSInteger current;
@property (nonatomic, assign) NSInteger totalCurrent;

@end

@implementation MyRedenvelopesUsed

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
    [self loadData];
}
- (void)initCSS{
    self.dataArr = [[NSMutableArray alloc]init];
    self.current = 1;
    self.totalCurrent = 1;
    self.RedStatus = @"2";
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.sectionFooterHeight = 0.01f;
    self.tableView.sectionHeaderHeight = 10.f;
    [self.tableView registerNib:[UINib nibWithNibName:@"MyRedenvelopesCell" bundle:nil] forCellReuseIdentifier:@"MyRedenvelopesCellID"];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.current = 1;
        self.dataArr = [[NSMutableArray alloc]init];
        if (self.pushView) {
            [self.pushView removeFromSuperview];
        }
        [self loadData];
    }];
    self.tableView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        self.current += 1;
        if (self.current <= self.totalCurrent) {
            if (self.pushView) {
                [self.pushView removeFromSuperview];
            }
            [self loadData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.top.equalTo(self.view);
    }];
}
- (void)loadData{
    [[[MyRedenvelopesStore alloc]init] GetDetailWithStatus:self.RedStatus AndSize:@"100" AndCurrent:[NSString stringWithFormat:@"%ld",self.current] Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            self.totalCurrent = [[NSString stringWithFormat:@"%@",res[@"data"][@"pages"]] integerValue];
            NSArray *arr = res[@"data"][@"records"];
            if (arr.count > 0) {
                for (NSDictionary *dic in arr) {
                    MyRedenvelopesModel *model = [MyRedenvelopesModel mj_objectWithKeyValues:dic];
                    model.uid = [NSString stringWithFormat:@"%@",dic[@"id"]];
                    [self.dataArr addObject:model];
                }
            }else{
                self.pushView = [[PushView alloc]initWithMyRedenvelopes];
                [self.tableView addSubview:self.pushView];
            }
        }else{
            [self showMBPError:res[@"msg"]];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } Failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    header.backgroundColor = [UIColor colorWithHexString:@"F0F0F0"];
    return header;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyRedenvelopesCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"MyRedenvelopesCellID"];
    Cell.CellType = [self.RedStatus integerValue];
    if (self.dataArr.count > 0) {
        Cell.model = self.dataArr[indexPath.section];
    }
    Cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return Cell;
}

@end
